//
//  GuestSearchRecordView.m
//  penzer
//
//  Created by 还带大道 on 2018/11/8.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "GuestSearchRecordView.h"

@interface GuestSearchRecordView()
@property (strong, nonatomic) NSMutableArray<UIButton *> *btn_items;

@end

@implementation GuestSearchRecordView

static int const kMaxCount = 15;
static NSString *const kHistorySearch = @"historySearch";

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
        [self addItems];
    }
    return self;
}



- (void)layoutSubviews
{
    [super layoutSubviews];
    if(_btn_items.count > 0){
        [self updateFrame];
    }
}



- (void)addItems
{
    [self addItems:[RZ_USERDEFAULTES objectForKey:kHistorySearch]];
}



- (void)addItems:(NSArray<NSString *> *)items
{
    if(items.count == 0) return;
    [items enumerateObjectsUsingBlock:^(NSString * _Nonnull str, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = nil;
        if(idx < self.btn_items.count){
            btn = self.btn_items[idx];
        }else{
            btn = [UIButton new];
            [self addSubview:btn];
            [self.btn_items addObject:btn];
        }
    
        [btn addTarget:self action:@selector(itemOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:str forState:UIControlStateNormal];
        [btn setTitleColor:RZ_COLOR(51, 51, 51) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.backgroundColor = RZ_COLOR(245, 245, 245);
    }];
    [self updateFrame];
}



- (void)updateFrame
{
    CGFloat margin_xy = 15;
    CGFloat btn_h = 32;
    __block CGFloat btn_w = 0;
    __block CGFloat btn_y = 0;
    __block  CGFloat btn_x = margin_xy;
    [self.btn_items enumerateObjectsUsingBlock:^(UIButton * _Nonnull btn, NSUInteger idx, BOOL * _Nonnull stop) {
        [btn sizeToFit];
        btn_w = btn.frame.size.width + 24;
        if(btn_w > self.width - 2*margin_xy){
            btn_w = self.width - 2*margin_xy;
        }
        
        if(idx == 0){
            btn_y = margin_xy;
            btn_x = margin_xy;
        }else{
            btn_y = CGRectGetMinY(self.btn_items[idx-1].frame);
            btn_x = CGRectGetMaxX(self.btn_items[idx-1].frame) + margin_xy;
        }
        
        if(btn_x + btn_w  > (RZ_SCREEN_WIDTH-10)){
            btn_x = margin_xy;
            if(idx > 0){
                btn_y = CGRectGetMaxY(self.btn_items[idx-1].frame) + margin_xy;
            }
        }
        btn.frame = CGRectMake(btn_x, btn_y, btn_w, btn_h);
        
    }];
    
    CGFloat height = CGRectGetMaxY(self.btn_items.lastObject.frame) + margin_xy;
    self.height = height;
}



- (void)updateWithKeywords:(NSString *)keywords
{
    if(keywords.length < 1) return;
    NSMutableArray *items = [NSMutableArray arrayWithArray:[RZ_USERDEFAULTES objectForKey:kHistorySearch]];
    [items removeObject:keywords];
    [items insertObject:keywords atIndex:0];
    if(items.count > kMaxCount){
        [items removeLastObject];
    }
    
    //更新数据并持久保存
    [RZ_USERDEFAULTES setObject:items forKey:kHistorySearch];
    [RZ_USERDEFAULTES synchronize];
    
    [self addItems:items];
}



- (void)itemOnClick:(UIButton *)btn
{
    !self.itemOnClickBlock ? :self.itemOnClickBlock(btn.titleLabel.text);
}



#pragma mark--getter&&setter
- (NSMutableArray<UIButton *> *)btn_items
{
    if(!_btn_items){
        _btn_items = [NSMutableArray array];
    }
    return _btn_items;
}

@end
